<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>密码修改</title>
    <link rel="stylesheet" th:href="@{/layui/layui/css/layui.css}">
    <script th:src="@{/static/js/jquery-3.4.1.min.js}" type="text/javascript"></script>
    <script th:src="@{/layui/layui/layui.js}"></script>
</head>
<body>
<div class="layui-main" style="width: 50%;margin-top: 50px;">
    <h2>密码修改</h2>
    <span id="tishi1" style="color: #DAA5A6;display: none;">只有管理员才能修改邮箱用户名，普通用户不可以修改。</span>
    <br/>
    <form class="layui-form layui-form-pane" action="">
        <hr/>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱：</label>
            <div class="layui-input-block">
                <input id="email" type="text" name="title" autocomplete="off" disabled="true" class="layui-input"
                       th:value="${user.getEmail()}"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名：</label>
            <div class="layui-input-block">
                <input id="username" type="text" name="title" autocomplete="off" disabled="true" class="layui-input"
                       th:value="${user.getUsername()}"/>
            </div><span id="tishi" style="color: #DAA5A6;display: none;">用户名只可是英文或数字</span>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码：</label>
            <div class="layui-input-block">
                <input id="newpassword" type="text" name="title" autocomplete="off" placeholder="新密码"
                       class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码：</label>
            <div class="layui-input-block">
                <input id="registerpassword" type="text" name="title" autocomplete="off" placeholder="确认密码"
                       class="layui-input"/>
            </div>
        </div>
        <div class="layui-form-item">
            <a class="layui-btn" lay-submit="" lay-filter="demo2" onclick="change()">保存</a>
        </div>
    </form>
</div>
<script>
    var level = [[${user.getLevel()}]];
    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function (data) {
            layer.msg(JSON.stringify(data.field));
            return false;
        });
        $(function () {
            if(level==2){
                $('#username').attr("disabled",false);
                $('#email').attr("disabled",false);
                $('#tishi').css('display','block');
                $('#tishi1').css('display','block');
            }else{
                $('#username').attr("disabled",true);
                $('#email').attr("disabled",true);
                $('#tishi1').css('display','none');
                $('#tishi').css('display','none');
            }
        })
        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            if (this.checked == true) {
                imgreview(1);
            } else {
                imgreview(0);
            }
        });
    });
    function change() {
        var email = $("#email").val();
        var username = $("#username").val();
        var newpassword = $("#newpassword").val();
        var registerpassword = $("#registerpassword").val();
        if ((newpassword != null && newpassword != "") && (registerpassword != null && registerpassword != "")) {
            var usertype = /^(\d|[a-zA-Z])+$/;
            if (usertype.test(username) == false || username.length>10) {
                layer.msg("用户名格式错误", {icon: 2});
            }else{
                if (newpassword == registerpassword) {
                    var ze = /^\w+@\w+(\.[a-zA-Z]{2,3}){1,2}$/;
                    if (ze.test(email) == true) {
                        $.ajax({
                            type: "POST",
                            url: "/admin/change",
                            //data:{username:username,password:newpassword},
                            data: {email:email,username:username,password: newpassword},
                            dataType: "json",
                            success: function (data) {
                                if (data > 0) {
                                    layer.msg("修改成功,请重新登录。", {icon: 1});
                                    setTimeout(function () {
                                        exit();
                                    }, 2000);
                                }
                                if (data == "-1") {
                                    layer.msg("用户名重复。", {icon: 2});
                                }
                            }
                        });
                    }else{layer.msg("邮箱格式不正确。", {icon: 2});}
                } else {
                    layer.msg("两次密码输入不一致。", {icon: 2});
                }
            }
        } else {
            layer.msg("表单不能为空", {icon: 2});
        }
    }
    function exit() {
        $.ajax({
            type: "POST",
            url: "/user/exit.do",
            dataType: "json",
            success: function (data) {
                parent.location.reload();//刷新父元素
            }
        });
    }
</script>
</body>
</html>